package com.example.administrator.xtu_community.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.xtu_community.Adapter.CommentsRecyclerViewAdapter;
import com.example.administrator.xtu_community.R;
import com.example.administrator.xtu_community.bean.Comment;
import com.example.administrator.xtu_community.bean.Note;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageListener;

import java.util.List;

public class DetailsActivity extends AppCompatActivity {

    CarouselView carouselView;
    TextView title;
    TextView content;
    ImageView back;
    RecyclerView recyclerView;
    EditText comment;
    Button commentPush;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        Note note = (Note)getIntent().getSerializableExtra("note");
        List<Integer> imageList = note.getShow_images();
        List<String> imageList2 = note.getImage2List();
        Log.d("TAG", "onCreate: " + imageList2.size());
        carouselView = findViewById(R.id.carouselView);
        Log.d("TAG", "onCreate: ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                carouselView.setImageListener(new ImageListener() {
                    @Override
                    public void setImageForPosition(int position, ImageView imageView) {
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        if(note.getNote_from() == 2) {
                            Log.d("TAG", "from == 2: ");
                            Glide.with(DetailsActivity.this).load(Uri.parse(imageList2.get(position))).into(imageView);
                        } else {
                            Log.d("TAG", "from == 1: ");
                            Glide.with(DetailsActivity.this).load(imageList.get(position)).into(imageView);
                        }
                    }
                });
                if(note.getNote_from() == 2) {
                    carouselView.setPageCount(imageList2.size());
                } else {
                    carouselView.setPageCount(imageList.size());
                }

            }
        }).start();

        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        title.setText(note.getTitle());
        content.setText(note.getContent());
        back = findViewById(R.id.backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView = findViewById(R.id.comment_recyclerview);
        CommentsRecyclerViewAdapter adapter1 = new CommentsRecyclerViewAdapter(note.getCommentList());
        recyclerView.setAdapter(adapter1);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        comment = findViewById(R.id.comment_text);
        commentPush = findViewById(R.id.comment_btn);
        commentPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = comment.getText().toString();
                Comment comment1 = new Comment("1",text);
                note.getCommentList().add(comment1);
                comment1.save();
                note.save();
                adapter1.refresh(note.getCommentList());
            }
        });
    }

}